import cTitle from "components/title";
import diyBackGround from "../balance/componet/diyBackGround.vue"
import { Toast } from "vant";
import yzGoodsposter from "../../../components/ui_components/yz_goodsPoster.vue"
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      toi: this.fun.getKeyByI(),
      credit1: 0.0,
      transfer: false, //是否开启转账
      integral: window.localStorage.integral,
      activity: false, //是否开启积分活动
      lan_name: "",		//蓝牛积分名称
      transfer_integral: false,
      transfer_integral_text: "",
      point_name: "",
      lan_plugin: false,		//蓝牛积分是否开启
      exchange_to_love_by_member: 0,  //积分转爱心值是否开启
      love: '',
      // 积分弹窗
      show: false,
      list: [],
      //积分明细
      dataList: [],
      other: {},

      // 海报
      posterShow: false,
      poster: "",
      poster_Data: {
        poster_data: null,
        background: {}
      },
      poster_id: null, //海报id
      isNewPosterPlugin: false, //是否为新海报
      listIncome: 0,
      integralTaskName: '',

      //more
      isLoadMore: true,
      page: 1,
      total_page: 0,
    };
  },
  methods: {
    onEmit(e){
      console.log(e,'ddddddddddddddddddddd')
      this.poster ="";
      this.poster_Data.poster_data = "";
      this.timer = "";
    },
    tapOther(item, index) {

      if (index == 'activity') {
        this.toIntegral();
      } else if (index == 'transfer_integral') {
        this.to_transfer_integral();
      } else if (index == 'exchange_to_love_by_member') {
        this.to_love_member();
      } else if (index == 'lan_plugin') {
        this.toIntegralBlueCattle();
      }else if(index == 'point_exchange'){
        this.point_exchange()
      }
    },
    point_exchange(){
      this.$router.push(this.fun.getUrl('intergral_transform'))
    },
    to_transfer_integral() {
      this.$router.push(this.fun.getUrl("member_transfer_integral"))
    },
    //转账的响应方法
    transferHandel() {
      if (this.transfer) {
        this.$router.push({
          name: "integral_v2_transfer",
          params: { integral: this.credit1 },
          query: { i: this.toi }
        });
      } else {
        this.$dialog.alert({ message: "您的转账功能未开启" });

      }
    },
    to_love_member() {
      this.$router.push({
        name: "member_transfer_integral",
        query: { i: this.toi, type: this.fun.getTyep(), loveByMember: this.exchange_to_love_by_member }
      });
    },
    tourl(key, query) {
      this.$router.push(
        this.fun.getUrl(key, query)
      );
    },
    toIntegral() {
      this.$router.push(
        this.fun.getUrl("IntegralActivity", {
          integral: this.credit1,
          i: this.toi
        })
      );
    },
    toIntegralBlueCattle() {
      this.$router.push(
        this.fun.getUrl("integralBlueCattle", {
          integral: this.credit1,
          i: this.toi
        })
      );
    },
    get_transfer_integral() {
      $http.post("finance.point-transfer-integral.page-per").then(res => {
        if (res.result) {
          console.log('执行');
          this.transfer_integral_text = res.data.point_name + '转化' + res.data.integral_name;
          this.point_name = res.data.point_name;
        }
      });
    },
    getIntegral() {
      var that = this;
      $http
        .get(
          "finance.point-page.index",
          {
            i: this.fun.getKeyByI(),
            type: this.fun.getTyep()
          },
          "加载中"
        )
        .then(
          function (response) {
            if (response.result == 1) {
              console.log(response.data);
              that.credit1 = response.data.credit1;
              that.transfer = response.data.transfer;
              that.lan_name = response.data.lan_name;
              that.transfer_integral = response.data.transfer_integral;
              that.dataList = response.data.latest_record;
              that.other = response.data.other;
              that.point_task_switch = response.data.point_task_switch;
              if(that.other.exchange_to_love_by_member){
                that.exchange_to_love_by_member = 1;
              }
              if (that.point_task_switch) {
                that.getTaskList();
              }
              if (response.data.transfer_integral) {
                that.get_transfer_integral();
              }
              if (response.data.activity == 1) {
                that.activity = true;
              } else {
                that.activity = false;
              }
              if (response.data.lan_plugin == 1) {
                that.lan_plugin = true;
              }
            } else {
              that.$dialog.alert({ message: response.msg });

            }
          },
          function (response) {
            that.$dialog.alert({ message: response.msg });

          }
        );
    },
    getTaskList() {
      $http.post("plugin.point-task.api.task.index", { page: this.page }, ' ').then(res => {
        if (res.result) {
          console.log('res', res.data.list);
          this.list = res.data.list;
          this.listIncome = res.data.income;
          this.isLoadMore = true;
          this.integralTaskName = res.data.name ? res.data.name : '积分任务';
          this.total_page = res.data.last_page;
          if (!this.total_page) {
            this.total_page = 0;
          }
        } else {
          this.$dialog.alert({ message: res.msg });
        }
      });
    },
    getMoreData() {
      if (!this.isLoadMore) return;
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        $http.post("plugin.point-task.api.task.index", { page: this.page }).then(response => {
          if (response.result === 1) {
            var nextPageData = response.data.list;
            this.list = this.list.concat(nextPageData)
            this.isLoadMore = true;
            console.log(this.record_list.data);
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
          }
        });
      }
    },
    // 积分任务按钮
    jumpUrlBtn(item) {
      console.log(item, 'ddddddd')
      if (item.type == 1) {
        // 自定义链接
        window.location.href = item.url
      } else if (item.type == 2) {
        // 广告
        if (item.poster_id) {
          this.getPoster(item.poster_id)
        }
      } else if (item.type == 3) {
        this.$router.push(this.fun.getUrl("editmobile"));
      }else {
        window.location.href = item.url
      }
    },
    uploadImageM(e) {
      // console.log(e)
    },
    getPoster(poster_id) {
      if (!this.fun.isTextEmpty(this.poster_Data.poster_data) || !this.fun.isTextEmpty(this.poster)) {
        this.posterShow = true;
        return;
      }
      $http.get("member.qrcode.get-poster", { poster_id: poster_id }, {}).then(
        response => {
          if (response.result === 1) {
            this.isNewPosterPlugin = response.data.new || false;
            if (response.data.center_show == 0) {
              //0 返回的默认图片
              this.poster = response.data.image_url;
              this.posterShow = true;
            } else if (response.data.center_show == 1) {
              //前端生成
              this.poster_Data.background.src = response.data.background_url || response.data.background;
              this.poster_Data.background.type = "background";
              this.poster_Data.poster_data = response.data.style_data;
              this.poster_id = response.data.id;
              this.posterShow = true;
              console.log(this.poster_Data)
            } else if (response.data.center_show == 2) {
              //部分关注二维码不支持跨域，走老接口
              this.toastPoster = Toast({
                duration: -1, // 持续展示 toast
                message: "正在为您生成海报中"
              });
              // member.poster新增poster_id传参
              this.openQrCode_old(response.data.poster_id);
            }
          } else {
            Toast(response.msg);
          }
        },
        response => {
          console.log(response.msg);
          if (this.timer) {
            clearInterval(this.timer);
            if (this.toastPoster) {
              this.toastPoster.close();
            }
          }
        }
      );
    },
    openQrCode_old(id) {
      if (!this.poster) {
        if (!this.timer) {
          this.getPoster_oldApi(id);
          this.timer = setInterval((id) => {
            this.getPoster_oldApi(id);
          }, 3000);
        }
      } else {
        clearInterval(this.timer);
        this.toastPoster.close();
      }
    },
    getPoster_oldApi(id) {
      $http.get("member.poster",{poster_id:id}).then(
        response => {
          if (response.result === 1) {
            this.poster = response.data.image_url;
            if (this.poster && this.timer) {
              this.posterShow = true;
              clearInterval(this.timer);
              if (this.toastPoster) {
                this.toastPoster.close();
              }
            }
          } else {
            Toast(response.msg);
            if (this.timer) {
              this.posterShow = true;
              clearInterval(this.timer);
              if (this.toastPoster) {
                this.toastPoster.close();
              }
            }
          }
        },
        response => {
          console.log(response.msg);
          if (this.timer) {
            this.posterShow = true;
            clearInterval(this.timer);
            if (this.toastPoster) {
              this.toastPoster.close();
            }
          }
        }
      );
    },

  },

  activated() {
    let basic_info = JSON.parse(localStorage.getItem("yz_basic_info"));
    this.love = basic_info.love;
    this.toi = this.fun.getKeyByI();
    this.getIntegral();
    this.fun.setWXTitle(this.integral);
  },
  components: { cTitle, diyBackGround, yzGoodsposter }
};
